import QtQuick 1.1
import com.nokia.meego 1.0
import QuickGestures 1.0
import "HarmattanUIConstants.js" as Constants

Page {
    id: editorPage

    property int gestureId: -1
    property bool gestureIsFromList: false
    property bool modified: false

    /**
     * Sets the gesture by the given ID.
     *
     * @param id The gesture ID. If the given value is less than zero, the
     *           gesture is cleared.
     */
    function setGestureId(id)
    {
        console.debug("EditorPage.qml: setGestureId():", id);
        gestureId = id;

        if (gestureId >= 0) {
            if (helper.copy(unprocessedGesturesModel.gestureById(gestureId), editorGesture)) {
                gestureIsFromList = true;
            }
            else {
                console.debug("EditorPage.qml: setGestureId(): Failed to copy the gesture data!");
            }
        }
        else {
            editorGesture.clear();
            editorGesture.id = Math.random() * 32000;
            gestureIsFromList = false;
        }

        updateControls();
    }

    /**
     * Updates the controls according to the attributes of editorGesture.
     */
    function updateControls()
    {
        idTextField.text = editorGesture.id;
        nameTextField.text = editorGesture.name;
        startingPointCheckBox.checked =
                (editorGesture.attributes & Gesture.FixedStartingPoint);
        directionCheckBox.checked =
                (editorGesture.attributes & Gesture.FixedDirection);
    }

    /**
     * Saves the changes.
     */
    function save()
    {
        console.debug("EditorPage.qml: save()");

        if (!modified) {
            console.debug("EditorPage.qml: save():",
                          "The current gesture has not been modified!");
            return;
        }

        if (gestureIsFromList) {
            if (!helper.update(editorGesture, unprocessedGesturesModel)) {
                console.debug("EditorPage.qml: save():",
                              "Failed to update the changes!");
            }
        }
        else {
            if (!helper.copyAndAppend(editorGesture, unprocessedGesturesModel)) {
                console.debug("EditorPage.qml: save():",
                              "Failed to append a new gesture!");
            }

            gestureIsFromList = true;
        }

        if (!helper.copyAndProcess(editorGesture, gestureModel)) {
            console.debug("EditorPage.qml: save():",
                          "Failed to reprocess the gesture!");
        }

        gestureListPage.setModified(gestureId);
        modified = false;
    }

    Connections {
        target: editorGesture

        onPointCountChanged: {
            if (count) {
                editorPage.modified = true;
            }
        }
    }

    HeaderLabel {
        id: gestureSettingsLayout
        height: textFieldRow.height + secondRow.height
                + thirdRow.height;

        Column {
            id: mainColumn
            anchors.fill: parent

            Row {
                id: textFieldRow
                x: Constants.DEFAULT_MARGIN
                width: mainColumn.width - Constants.DEFAULT_MARGIN * 2
                height: Constants.DEFAULT_BAR_HEIGHT
                spacing: Constants.DEFAULT_MARGIN

                Text {
                    id: idLabel
                    width: 30
                    height: textFieldRow.height
                    color: Constants.DEFAULT_TEXT_COLOR
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                    text: "ID"
                }

                TextField {
                    id: idTextField
                    y: (textFieldRow.height - height) / 2
                    width: 100
                    height: Constants.DEFAULT_TEXT_FIELD_HEIGHT;
                    validator: IntValidator { bottom: 0 }

                    onTextChanged: {
                        editorGesture.id = text;
                        editorPage.gestureId = editorGesture.id;

                        if (gestureModel.gestureById(editorGesture.id)) {
                            editorPage.gestureIsFromList = true;
                        }
                        else {
                            editorPage.gestureIsFromList = false;
                        }

                        editorPage.modified = true;
                    }
                }

                Text {
                    id: nameLabel
                    width: 60
                    height: textFieldRow.height
                    color: Constants.DEFAULT_TEXT_COLOR
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
                    text: "Name"
                }

                TextField {
                    id: nameTextField
                    y: (textFieldRow.height - height) / 2
                    width: textFieldRow.width - idLabel.width - idTextField.width
                           - nameLabel.width - Constants.DEFAULT_MARGIN * 3.5;
                    height: Constants.DEFAULT_TEXT_FIELD_HEIGHT;

                    onTextChanged: {
                        editorGesture.name = text;
                        editorPage.modified = true;
                    }
                }
            }

            Row {
                id: secondRow
                width: parent.width
                height: Constants.DEFAULT_BAR_HEIGHT

                CheckBox {
                    id: startingPointCheckBox
                    width: parent.width - saveButton.width
                    text: "Fixed starting point"
                    checked: (editorGesture.attributes & Gesture.FixedStartingPoint)

                    onClicked: {
                        if (startingPointCheckBox.checked) {
                            editorGesture.attributes =
                                    Gesture.FixedStartingPoint
                                    | Gesture.FixedDirection;
                            directionCheckBox.checked = true;
                        }
                        else {
                            editorGesture.attributes =
                                    (editorGesture.attributes
                                     & ~Gesture.FixedStartingPoint);
                        }

                        console.debug("editorGesture.attributes ==",
                                      editorGesture.attributes);
                        editorPage.modified = true;
                    }
                }

                Button {
                    id: saveButton
                    x: secondRow.width - width - Constants.DEFAULT_MARGIN
                    y: (secondRow.height - height) / 2
                    width: 150
                    height: Constants.DEFAULT_BUTTON_HEIGHT
                    text: "Save"
                    enabled: (editorPage.modified && editorGesture.pointCount)

                    onClicked: {
                        editorPage.save();
                    }
                }
            }

            Row {
                id: thirdRow
                width: parent.width
                height: Constants.DEFAULT_BAR_HEIGHT

                CheckBox {
                    id: directionCheckBox
                    width: parent.width - clearButton.width
                    text: "Fixed direction"
                    checked: (editorGesture.attributes & Gesture.FixedDirection)
                    enabled: !startingPointCheckBox.checked

                    onClicked: {
                        if (directionCheckBox.checked) {
                            editorGesture.attributes |= Gesture.FixedDirection;
                        }
                        else {
                            editorGesture.attributes =
                                    (editorGesture.attributes
                                     & ~Gesture.FixedDirection);
                        }

                        console.debug("editorGesture.attributes ==",
                                      editorGesture.attributes);
                        editorPage.modified = true;
                    }
                }

                Button {
                    id: clearButton
                    x: thirdRow.width - width - Constants.DEFAULT_MARGIN
                    y: (thirdRow.height - height) / 2
                    width: 150
                    height: Constants.DEFAULT_BUTTON_HEIGHT
                    text: "Clear"
                    enabled: editorGesture.pointCount

                    onClicked: {
                        editorGesture.clear();
                        editorGesture.id = Math.random() * 32000;
                        idTextField.text = editorGesture.id;
                    }
                }
            }
        }
    }

    GestureVisualizer {
        id: sketchVisualizer

        anchors {
            top: gestureSettingsLayout.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }

        autoScale: false

        Component.onCompleted: {
            editorGesture.clear();
            setGesture(editorGesture);
        }
    }

    MouseArea {
        id: editorPageMouseArea

        anchors {
            top: gestureSettingsLayout.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }

        z: 1

        onPressed: {
            if (editorGesture.pointCount) {
                // Add a break point.
                editorGesture.appendPoint(-1, -1);
            }

            editorGesture.appendPoint(mouse.x, mouse.y);

            if (!editorPage.modified) {
                editorPage.modified = true;
            }
        }
        onMousePositionChanged: {
            editorGesture.appendPoint(mouse.x, mouse.y);
        }
    }

    // Tool bar
    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: pageStack.pop();
        }
    }

    Component.onCompleted: {
        editorGesture.id = Math.random() * 32000;
        updateControls();
    }
}

// End of file.
